package com.dj.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dj.ssm.entity.*;
import com.dj.ssm.mapper.ExamMapper;
import com.dj.ssm.mapper.ExamineeMapper;
import com.dj.ssm.mapper.PersonMapper;
import com.dj.ssm.service.ExamService;
import com.dj.ssm.service.dto.ExamDto;
import com.dj.ssm.util.BusinessException;
import com.dj.ssm.util.DozerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private ExamineeMapper examineeMapper;

    @Autowired
    private PersonMapper personMapper;

    /**
     * 展示
     */
    @Override
    public List<ExamQ> queryAll() {
        List<ExamQ> exam = examMapper.queryList();
        //当前时
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        for (ExamQ aa : exam) {
            //开始时间在当前时之后 未开始
            if(aa.getStartTime().isAfter(now)){
                aa.setStatus("未开始");
               //结束时间在当前时之前 已结束
            }else if(aa.getEndTime().isBefore(now)){
                aa.setStatus("已结束");
            }else{
                aa.setStatus("进行中");
            }
        }
        return exam;
    }

    /**
     * 新增
     *
     * @param examDto
     */
    @Override
    public void insertAll(ExamDto examDto) {
        Exam exam = DozerUtil.map(examDto, Exam.class);
        examMapper.insert(exam);
    }

    /**
     * 新增
     * @param examinee
     * @param
     */
    @Override
    public void insertUser(Examinee examinee,Integer examId) {
        QueryWrapper<Examinee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", examinee.getUserName());
        Examinee user = examineeMapper.selectOne(queryWrapper);
        if(user != null){
            throw new BusinessException("同一个考生不可重复录入");
        }
        examineeMapper.insert(examinee);
        Person person = new Person();
        person.setExamId(examId);
        person.setNeeId(examinee.getId());
        personMapper.insert(person);
    }

    /**
     * 展示
     *
     * @param examId
     */
    @Override
    public List<Examinee> queryUser(Integer examId) {
        List<Examinee> examinees = examineeMapper.queryUser(examId);
        return examinees;
    }

    /**
     * 去展示
     *
     * @param examId
     * @return
     */
    @Override
    public ExamQ queryExam(Integer examId) {
        return examMapper.queryById(examId);
    }

    @Override
    public void deleteById(Integer neeId) {
        ExamineeQ aa = personMapper.queryNeeId(neeId);
        LocalDateTime now = LocalDateTime.now();
            //开始时间在当前时之后 未开始
            if(!aa.getStartTime().isAfter(now)){
                throw new BusinessException("只有未开始的考试可以移除人员 ");
            }

        personMapper.deleteByneeId(neeId);
    }

}
